﻿using DelonixRegia.Classes;
using DelonixRegia.Classes.RoomRecords;
using DelonixRegia.Classes.Rooms;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DelonixRegia
{
    public partial class ModifyGuestRecord : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["id"] != null)
            {
                tbxGuestID.Text = (string)Session["id"];
                tbxGuestFirstName.Text = (string)Session["firstname"];
                tbxGuestLastName.Text = (string)Session["lastname"];
                tbxGuestAdultNo.Text = (string)Session["adultno"];
                tbxGuestChildrenNo.Text = (string)Session["childrenno"];
                tbxGuestContactNumber.Text = (string)Session["contactno"];
                tbxGuestEmailAddress.Text = (string)Session["email"];
                tbxGuestHomeAddress.Text = (string)Session["address"];
                tbxHotelRoomNumber2.Text = (string)Session["roomno"];
                tbxGuestCardholderName.Text = (string)Session["cardname"];
                ddlGuestCardType.SelectedValue = (string)Session["cardtype"];
                tbxGuestCardNo.Text = (string)Session["cardno"];               
                tbxGuestCSC.Text = (string)Session["csc"];
                tbxPaymentID.Text = (string)Session["paymentid"];

                string cardexpiryDate = (string)Session["expiry"];
                string[] expirydateparts = cardexpiryDate.Split('/');
                string expirymonth = expirydateparts[0];
                string expiryyear = expirydateparts[1];
                ddlGuestExpiryMonth.SelectedValue = expirymonth;
                ddlGuestExpiryYear.SelectedValue = expiryyear;

                pnlFirstStep.Visible = false;
                pnlSecondStep.Visible = true;
                Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbxGuestContactNo2.Text))
            {
                Classes.RoomBooking rb = RoomRecordsDBManager.GetGuestByContactNo(tbxGuestContactNo2.Text);

                if (rb.contactNo != null)
                {
                    tbxGuestID.Text = rb.bookingID.ToString();
                    tbxGuestFirstName.Text = rb.firstName.ToString();
                    tbxGuestLastName.Text = rb.lastName.ToString();
                    tbxGuestAdultNo.Text = rb.noOfAdults.ToString();
                    tbxGuestChildrenNo.Text = rb.noOfChildren.ToString();
                    tbxGuestContactNumber.Text = rb.contactNo.ToString();
                    tbxGuestEmailAddress.Text = rb.emailAddress.ToString();
                    tbxGuestHomeAddress.Text = rb.address.ToString();

                    ddlGuestCardType.SelectedValue = rb.paymentDetails.cardType.ToString();
                    tbxGuestCardholderName.Text = rb.paymentDetails.cardholderName.ToString();
                    tbxGuestCardNo.Text = rb.paymentDetails.cardNo.ToString();
                    tbxPaymentID.Text = rb.paymentDetails.paymentID.ToString();

                    string expirydate = rb.paymentDetails.expiryDate.ToString();
                    string[] expirydateparts = expirydate.Split('/');
                    string expirymonth = expirydateparts[0];
                    string expiryyear = expirydateparts[1];

                    ddlGuestExpiryMonth.SelectedValue = expirymonth;
                    ddlGuestExpiryYear.SelectedValue = expiryyear;
                    tbxGuestCSC.Text = rb.paymentDetails.securityCode.ToString();

                    pnlFirstStep.Visible = false;
                    pnlSecondStep.Visible = true;
                    Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
                }
                else
                {
                    ClientScriptMgr.showErrorBox("The Guest Information you entered is not available or does not exist!");
                }
            }
            else if (!string.IsNullOrEmpty(tbxGuestName2.Text))
            {
                Classes.RoomBooking rbb = RoomRecordsDBManager.GetGuestByName(tbxGuestName2.Text);

                if (rbb.contactNo != null)
                {
                    tbxGuestID.Text = rbb.bookingID.ToString();
                    tbxGuestFirstName.Text = rbb.firstName.ToString();
                    tbxGuestLastName.Text = rbb.lastName.ToString();
                    tbxGuestAdultNo.Text = rbb.noOfAdults.ToString();
                    tbxGuestChildrenNo.Text = rbb.noOfChildren.ToString();
                    tbxGuestContactNumber.Text = rbb.contactNo.ToString();
                    tbxGuestEmailAddress.Text = rbb.emailAddress.ToString();
                    tbxGuestHomeAddress.Text = rbb.address.ToString();

                    ddlGuestCardType.SelectedValue = rbb.paymentDetails.cardType.ToString();
                    tbxGuestCardholderName.Text = rbb.paymentDetails.cardholderName.ToString();
                    tbxGuestCardNo.Text = rbb.paymentDetails.cardNo.ToString();
                    tbxPaymentID.Text = rbb.paymentDetails.paymentID.ToString();

                    string expirydate = rbb.paymentDetails.expiryDate.ToString();
                    string[] expirydateparts = expirydate.Split('/');
                    string expirymonth = expirydateparts[0];
                    string expiryyear = expirydateparts[1];

                    ddlGuestExpiryMonth.SelectedValue = expirymonth;
                    ddlGuestExpiryYear.SelectedValue = expiryyear;
                    tbxGuestCSC.Text = rbb.paymentDetails.securityCode.ToString();

                    pnlFirstStep.Visible = false;
                    pnlSecondStep.Visible = true;
                    Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
                }
                else
                {
                    ClientScriptMgr.showErrorBox("The Guest Information you entered is not available or does not exist!");
                }
            }
            else
            {
                ClientScriptMgr.showErrorBox("Please enter the following Guest Information!");
            }
        }

        protected void btnEditRoomDetails_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbxGuestName2.Text))
            {
                Classes.RoomBooking rb = RoomRecordsDBManager.GetGuestByName(tbxGuestName2.Text);
                if (rb != null)
                {
                    Classes.Room r = RoomRecordsDBManager.GetRoomDetail(rb.roomNo);
                    tbxHotelRoomNumber2.Text = r.roomNo.ToString();
                    ddlGuestRoomType.SelectedValue = r.roomType.ToString();
                    if (r.balcony.ToString().Equals("False"))
                    {
                        rblRoomBalcony.SelectedIndex = 1;
                    }
                    else
                    {
                        rblRoomBalcony.SelectedIndex = 0;
                    }

                    if (r.smoking.ToString().Equals("True"))
                    {
                        rblRoomSmoke.SelectedIndex = 0;
                    }
                    else
                    {
                        rblRoomSmoke.SelectedIndex = 1;
                    }
                    if (r.roomLevel == 3)
                    {
                        rblRoomLevel.SelectedIndex = 1;
                    }
                    else
                    {
                        rblRoomLevel.SelectedIndex = 0;
                    }
                }
            }
            else if (!string.IsNullOrEmpty(tbxGuestContactNo2.Text))
            {
                Classes.RoomBooking rb2 = RoomRecordsDBManager.GetGuestByContactNo(tbxGuestContactNo2.Text);
                if (rb2 != null)
                {
                    Classes.Room r = RoomRecordsDBManager.GetRoomDetail(rb2.roomNo);
                    tbxHotelRoomNumber2.Text = r.roomNo.ToString();
                    ddlGuestRoomType.SelectedValue = r.roomType.ToString();
                    if (r.balcony.ToString().Equals("False"))
                    {
                        rblRoomBalcony.SelectedIndex = 1;
                    }
                    else
                    {
                        rblRoomBalcony.SelectedIndex = 0;
                    }

                    if (r.smoking.ToString().Equals("True"))
                    {
                        rblRoomSmoke.SelectedIndex = 0;
                    }
                    else
                    {
                        rblRoomSmoke.SelectedIndex = 1;
                    }
                    if (r.roomLevel == 3)
                    {
                        rblRoomLevel.SelectedIndex = 1;
                    }
                    else
                    {
                        rblRoomLevel.SelectedIndex = 0;
                    }
                }
            }

            else if (Session["roomno"] != null)
            {
                tbxHotelRoomNumber2.Text = (string)Session["roomno"];
                Classes.Room r = RoomRecordsDBManager.GetRoomDetail(Convert.ToInt32(tbxHotelRoomNumber2.Text));
                ddlGuestRoomType.SelectedValue = r.roomType.ToString();
                if (r.balcony.ToString().Equals("False"))
                {
                    rblRoomBalcony.SelectedIndex = 1;
                }
                else
                {
                    rblRoomBalcony.SelectedIndex = 0;
                }

                if (r.smoking.ToString().Equals("True"))
                {
                    rblRoomSmoke.SelectedIndex = 0;
                }
                else
                {
                    rblRoomSmoke.SelectedIndex = 1;
                }
                if (r.roomLevel == 3)
                {
                    rblRoomLevel.SelectedIndex = 1;
                }
                else
                {
                    rblRoomLevel.SelectedIndex = 0;
                }
            }

            pnlSecondStep.Visible = false;
            pnlThirdStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToThirdStep", "transitionToThirdStep();", true);
        }

        protected void btnCheckGuestRoom_Click(object sender, EventArgs e)
        {
            SqlCommand comm = new SqlCommand();
            comm.CommandText = "SELECT * FROM Room WHERE roomStatus='Available' AND (roomNo NOT IN (SELECT roomNo FROM RoomBooking WHERE bookingID=@bookingID)) AND capacity >=@capacity";
            comm.Parameters.AddWithValue("@bookingID", tbxGuestID.Text);
            comm.Parameters.AddWithValue("@capacity", (Convert.ToInt32(tbxGuestChildrenNo.Text)) + (Convert.ToInt32(tbxGuestAdultNo.Text)));

            if (ddlGuestRoomType.SelectedValue != null)
            {
                comm.CommandText += " AND roomType=@roomType";
                comm.Parameters.AddWithValue("@roomType", ddlGuestRoomType.SelectedValue);
            }

            if (rblRoomBalcony.SelectedItem != null)
            {
                comm.CommandText += " AND balcony=@balcony";
                int balcony = 0;
                if (rblRoomBalcony.SelectedItem.Text.Equals("Yes")) balcony = 1;
                else if (rblRoomBalcony.SelectedItem.Text.Equals("No")) balcony = 0;
                comm.Parameters.AddWithValue("@balcony", balcony);
            }

            if (rblRoomSmoke.SelectedItem != null)
            {
                comm.CommandText += " AND smoking=@smoking";
                int smoke = 0;
                if (rblRoomSmoke.SelectedItem.Text.Equals("Smoking")) smoke = 1;
                else if (rblRoomSmoke.SelectedItem.Text.Equals("Non-Smoking")) smoke = 0;
                comm.Parameters.AddWithValue("@smoking", smoke);
            }

            if (rblRoomLevel.SelectedItem != null)
            {
                if (rblRoomLevel.SelectedItem.Text.Equals("High Level"))
                {
                    comm.CommandText += " AND roomLevel > 2";
                }
                else if (rblRoomLevel.SelectedItem.Text.Equals("Low Level"))
                {
                    comm.CommandText += " AND roomLevel <= 2";
                }
            }

            IEnumerable<Room> availableRooms = RoomDBManager.getAvailableRoom(comm);
            ArrayList list = new ArrayList();

                foreach (Room r in availableRooms)
                {
                    ViewRoom vr = new ViewRoom();
                    vr.RoomNumber = r.roomNo;
                    vr.RoomLevel = r.roomLevel;
                    vr.RoomType = RoomDBManager.getRoomType(r.roomType).name;
                    vr.RoomRate = r.roomRate;
                    list.Add(vr);
                }

                gvData.DataSource = list.Cast<ViewRoom>();
                gvData.DataBind();

                if (list.Count != 0)
                {
                    pnlThirdStep.Visible = false;
                    pnlFourthStep.Visible = true;
                    Page.ClientScript.RegisterStartupScript(GetType(), "transitionToFourthStep", "transitionToFouthStep();", true);
                }
                else
                {
                    ClientScriptMgr.Execute("$('#searchErrorBox').html('<strong>Error:</strong> There are no rooms available!');$('#searchErrorBox').hide().show();");             
                }
        }

        protected void btnSaveDetails_Click(object sender, EventArgs e)
        {
            if (gvData.SelectedRow != null)
            {
                Classes.RoomBooking rb = new Classes.RoomBooking();
                Classes.PaymentDetail pd = new Classes.PaymentDetail();
                rb.bookingID = Convert.ToInt32(tbxGuestID.Text);
                rb.firstName = tbxGuestFirstName.Text;
                rb.lastName = tbxGuestLastName.Text;
                rb.noOfAdults = Convert.ToInt32(tbxGuestAdultNo.Text);
                rb.noOfChildren = Convert.ToInt32(tbxGuestChildrenNo.Text);
                rb.contactNo = tbxGuestContactNumber.Text;
                rb.emailAddress = tbxGuestEmailAddress.Text;
                rb.address = tbxGuestHomeAddress.Text;
                rb.roomNo = Convert.ToInt32(gvData.SelectedRow.Cells[1].Text);

                pd.paymentID = Convert.ToInt32(tbxPaymentID.Text);
                pd.cardholderName = tbxGuestCardholderName.Text;
                pd.cardNo = tbxGuestCardNo.Text;
                pd.securityCode = Convert.ToInt32(tbxGuestCSC.Text);
                pd.cardType = ddlGuestCardType.SelectedValue;
                pd.expiryDate = ddlGuestExpiryMonth.SelectedValue + "/" + ddlGuestExpiryYear.SelectedValue;

                if ((RoomRecordsDBManager.UpdateGuest(rb) == true) && (RoomRecordsDBManager.UpdatePayment(pd) == true))
                {
                    pnlFourthStep.Visible = false;
                    pnlFifthStep.Visible = true;
                    Page.ClientScript.RegisterStartupScript(GetType(), "transitionToFifthStep", "transitionToFifthStep();", true);
                }
                else
                {
                    ClientScriptMgr.Execute("$('#searchErrorBox').html('<strong>Error:</strong> Update Fail! Please review the guest record again!');$('#searchErrorBox').hide().show();");
                }
            }       
        }

        protected void tbxGuestCardNo_TextChanged(object sender, EventArgs e)
        {
            //codes
        }

        protected void btnSaveDetails2_Click(object sender, EventArgs e)
        {
            Classes.RoomBooking rb = new Classes.RoomBooking();
            Classes.PaymentDetail pd = new Classes.PaymentDetail();
            rb.bookingID = Convert.ToInt32(tbxGuestID.Text);
            rb.firstName = tbxGuestFirstName.Text;
            rb.lastName = tbxGuestLastName.Text;
            rb.noOfAdults = Convert.ToInt32(tbxGuestAdultNo.Text);
            rb.noOfChildren = Convert.ToInt32(tbxGuestChildrenNo.Text);
            rb.contactNo = tbxGuestContactNumber.Text;
            rb.emailAddress = tbxGuestEmailAddress.Text;
            rb.address = tbxGuestHomeAddress.Text;

            Classes.RoomBooking rb2 = RoomRecordsDBManager.GetGuestByContactNo(tbxGuestContactNo2.Text);
            Classes.Room r = RoomRecordsDBManager.GetRoomDetail(rb2.roomNo);
            tbxHotelRoomNumber2.Text = r.roomNo.ToString();
            rb.roomNo = Convert.ToInt32(tbxHotelRoomNumber2.Text);

            pd.paymentID = Convert.ToInt32(tbxPaymentID.Text);
            pd.cardholderName = tbxGuestCardholderName.Text;
            pd.cardNo = tbxGuestCardNo.Text;
            pd.securityCode = Convert.ToInt32(tbxGuestCSC.Text);
            pd.cardType = ddlGuestCardType.SelectedValue;
            pd.expiryDate = ddlGuestExpiryMonth.SelectedValue + "/" + ddlGuestExpiryYear.SelectedValue;

            if ((RoomRecordsDBManager.UpdateGuest(rb) == true) && (RoomRecordsDBManager.UpdatePayment(pd) == true))
            {
                pnlSecondStep.Visible = false;
                pnlFifthStep.Visible = true;
                Page.ClientScript.RegisterStartupScript(GetType(), "transitionToFifthStep", "transitionToFifthStep();", true);
            }
            else
            {
                ClientScriptMgr.Execute("$('#searchErrorBox').html('<strong>Error:</strong> Update Fail! Please review the guest record again!');$('#searchErrorBox').hide().show();");
            }
        }

        protected void btnGoBackPanel2_Click(object sender, EventArgs e)
        {
            tbxGuestContactNo2.Text = "";
            tbxGuestName2.Text = "";
            pnlSecondStep.Visible = false;
            pnlFirstStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToFirstStep", "transitionToFirstStep();", true);
            Session.Remove("id");
        }

        protected void btnGoBackPanel3_Click(object sender, EventArgs e)
        {
            tbxGuestContactNo2.Text = "";
            tbxGuestName2.Text = "";
            pnlThirdStep.Visible = false;
            pnlFirstStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToFirstStep", "transitionToFirstStep();", true);
            Session.Remove("id");
        }

        protected void btnGoBackPanel4_Click(object sender, EventArgs e)
        {
            tbxGuestContactNo2.Text = "";
            tbxGuestName2.Text = "";
            pnlFourthStep.Visible = false;
            pnlFirstStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToFirstStep", "transitionToFirstStep();", true);
            Session.Remove("id");
        }

        protected void btnGoBackPanel5_Click(object sender, EventArgs e)
        {
            tbxGuestContactNo2.Text = "";
            tbxGuestName2.Text = "";
            pnlFifthStep.Visible = false;
            pnlFirstStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToFirstStep", "transitionToFirstStep();", true);
            Session.Remove("id");
        }

    }
}